package idiet.dao.hibernate;

import idiet.bean.Unit;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository("unitDao")
public class UnitDao extends BaseDao {
	public Unit getUnitById(int unitId){
		return (Unit) this.sessionFactory
				.getCurrentSession()
				.createCriteria(Unit.class)
				.add(Restrictions.eq("id", unitId))
				.uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public List<Unit> getUnitsByUnitName(String name){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(Unit.class)
				.add(Restrictions.like("name",name))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<Unit> getUnits(){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(Unit.class)
				.list();
	}
	public void createUnit(Unit unit){
		this.sessionFactory
		.getCurrentSession()
		.save(unit);
	}
	public void updateUnit(Unit unit){
		this.sessionFactory
		.getCurrentSession()
		.update(unit);
	}
	public void deleteUnit(Unit unit){
		this.sessionFactory
		.getCurrentSession()
		.delete(unit);
	}
}
